import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../dd_core/dd_config.dart';
import '../../../dd_core/dd_other.dart';
import '../../../dd_models/dd_hive_works_model.dart';
import '../../../r.dart';
import '../../dd_personal_center/views/dd_view_background.dart';
import '../controllers/dd_comment_controller.dart';
import '../controllers/dd_home_second_controller.dart';
import '../views/dd_post_card.dart';
import 'dd_comment_page.dart';
import 'dd_user_space_page.dart';

class DDHomeSecondPage extends StatefulWidget {
  const DDHomeSecondPage({Key? key}) : super(key: key);

  @override
  State<DDHomeSecondPage> createState() => _DDHomeSecondPageState();
}

class _DDHomeSecondPageState extends State<DDHomeSecondPage> {
  DDHomeSecondController _controller = DDHomeSecondController();

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DDHomeSecondController>(
        init: _controller,
        initState: (state) {},
        dispose: (state) {},
        builder: (controller) {
          return Scaffold(
            appBar: AppBar(
              title: Text(''),
              leading: IconButton(
                icon: Image.asset(R.assetsImagesHomeSecond1),
                onPressed: () {
                  Get.back();
                },
              ),
            ),
            body: SafeArea(
              top: true,
              bottom: false,
              child: Obx(
                () => ListView.builder(
                        itemBuilder: (context, index) {
                          return DDPostCard(
                            model: controller.list[index],
                            onMore: () {
                              _showBottomSheet(context, controller.list[index]);
                            },
                            onComment: () {
                              showModalBottomSheet(
                                context: context,
                                isScrollControlled: true,
                                builder: (context) => Container(
                                  child: DDCommentPage(
                                    model: controller.list[index],
                                    commentController:
                                    Get.put(DDCommentController()),
                                  ),
                                ),
                              );
                            },
                            onLike: () {
                              controller.updateMyLike(controller.list[index]);
                            },
                            onCollection: () {
                              controller.updateFollow(controller.list[index]);
                            },
                            onClickIcon: () {
                              HiveWorksModel userModel = controller.list[index];
                              if(userModel.myPublish == true){
                                return;
                              }
                              DDRoutesConfig.push(
                                  DDUserSpacePage(
                                    userModel: userModel,
                                  ),
                                  routeName: "/userSpacePage");
                            },
                          );
                        },
                        itemCount: controller.list.length),
              ),
            ),
          );
        });
  }

  void _showBottomSheet(BuildContext context, HiveWorksModel model) {
    showModalBottomSheet(
      context: context,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(16.0)),
      ),
      builder: (BuildContext context) {
        return SafeArea(
            child: Container(
          padding: EdgeInsets.all(16.0),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              ListTile(
                title: Center(
                    child: Text( model.myFollow ?? false ? 'Unfollow' : 'Follow', style: TextStyle(fontSize: 16))),
                onTap: () {
                  // Add your follow action here
                  Navigator.pop(context);
                  _controller.updateFollow(model);
                },
              ),
              ListTile(
                title: Center(
                    child: Text('Block', style: TextStyle(fontSize: 16))),
                onTap: () {
                  // Add your block action here
                  Navigator.pop(context);
                  _controller.updateMyBlock(model);
                },
              ),
              ListTile(
                title: Center(
                    child: Text('Report', style: TextStyle(fontSize: 16))),
                onTap: () {
                  // Add your report action here
                  Navigator.pop(context);
                  _showBottomSheet1(context, model);
                },
              ),
              SizedBox(height: 16),
              DDViewBackGround(
                content: SizedBox(
                  width: 183,
                  height: 44,
                  child: DDSafeButton(
                    child: Container(
                      child: Text(
                        "Cancel",
                        style:
                            TextStyle(fontSize: 16, color: Color(0xff53BFD7)),
                      ),
                    ),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  ),
                ),
              ),
            ],
          ),
        ));
      },
    );
  }

  void _showBottomSheet1(BuildContext context, HiveWorksModel model) {
    showModalBottomSheet(
      context: context,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(16.0)),
      ),
      builder: (BuildContext context) {
        return SafeArea(
            child: Container(
          padding: EdgeInsets.all(16.0),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              ListTile(
                title: Center(
                    child:
                        Text('Pornographic', style: TextStyle(fontSize: 16))),
                onTap: () {
                  Navigator.pop(context);
                  _controller.report(model,'Pornographic');
                },
              ),
              ListTile(
                title: Center(
                    child:
                        Text('False gender', style: TextStyle(fontSize: 16))),
                onTap: () {
                  // Add your block action here
                  Navigator.pop(context);
                  _controller.report(model,'False gender');
                },
              ),
              ListTile(
                title: Center(
                    child: Text('Fraud', style: TextStyle(fontSize: 16))),
                onTap: () {
                  // Add your report action here
                  Navigator.pop(context);
                  _controller.report(model,'Fraud');
                },
              ),
              ListTile(
                title: Center(
                    child: Text('Political sensitive',
                        style: TextStyle(fontSize: 16))),
                onTap: () {
                  // Add your report action here
                  Navigator.pop(context);
                  _controller.report(model,'Political sensitive');
                },
              ),
              ListTile(
                title: Center(
                    child: Text('Other', style: TextStyle(fontSize: 16))),
                onTap: () {
                  // Add your report action here
                  Navigator.pop(context);
                  _controller.report(model,'Other');
                },
              ),
              SizedBox(height: 16),
              DDViewBackGround(
                content: Container(
                  width: 183,
                  height: 44,
                  child: DDSafeButton(
                    // padding: EdgeInsets.only(top: 20, bottom: 20),
                    child: Text(
                      "Cancel",
                      style: TextStyle(fontSize: 16, color: Color(0xff53BFD7)),
                    ),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  ),
                ),
              ),

              // TextButton(
              //   onPressed: () {
              //     Navigator.pop(context);
              //   },
              //   child: Text('Cancel', style: TextStyle(fontSize: 18, color: Colors.blue)),
              // ),
            ],
          ),
        ));
      },
    );
  }

  void _showSnackBar(String message, BuildContext context) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(message),
      ),
    );
  }
}
